#pragma semicolon 1
#include <sourcemod>
#include <sdkhooks>
#include <ncs_rpg>

#define VERSION		"ALPHA TEST"

new ThisSkillID;
new Float:SkillPercent;

new UseSDKHooks, SkillDeath;

public Plugin:myinfo =
{
	name		= "[NCS RPG] Skill Mirror Damage",
	author		= "[RUS] DiviX",
	description	= "Skill Mirror Damage for NCS RPG",
	version		= VERSION,
	url			= "N/A"
};

public OnPluginStart()
{
	HookEvent("player_hurt", Event_PlayerHurt);
}

public RPG_OnSkillsRegister()
{
	ThisSkillID = RegisterSkill("mirror", 5, 5, 10);
	UseSDKHooks = GetConfigValue("mirror", "sdkhooks", 0);
	SkillPercent = GetConfigValueFloat("mirror", "percent", 5.0)*0.01;
	SkillDeath = GetConfigValue("mirror", "death", 0);
}

public OnClientPutInServer(client)
{
	if(UseSDKHooks)
	{
		SDKHook(client, SDKHook_OnTakeDamagePost, OnTakeDamagePost);
	}
}

public Action:OnTakeDamagePost(victim, &attacker, &inflictor, &Float:damage, &damagetype)
{
	if(IsValidPlayer(victim) && IsValidPlayer(attacker) && victim != attacker)
	{
		if(GetClientTeam(victim) == GetClientTeam(attacker))
			return;
		
		new level = GetSkillLevel(victim, ThisSkillID);
		if(level > 0)
		{
			new amount = RoundToCeil(damage*SkillPercent*level);
			if(!SkillDeath && amount >= GetClientHealth(attacker))
				amount = GetClientHealth(attacker)-1;
			
			if(amount <= 0)
				return;
			
			DealDamage(attacker, amount, victim, _, "mirror");
		}
	}
}

public Event_PlayerHurt(Handle:event, const String:name[], bool:dontBroadcast)
{
	if(UseSDKHooks)
		return;
	
	new victim = GetClientOfUserId(GetEventInt(event, "userid"));
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	if(IsValidPlayer(victim) && IsValidPlayer(attacker) && victim != attacker)
	{
		new level = GetSkillLevel(victim, ThisSkillID);
		if(level > 0)
		{
			new Float:dmg_health = GetEventFloat(event, "dmg_health");
			new amount = RoundToCeil(dmg_health*SkillPercent*level);
			if(!SkillDeath && amount >= GetClientHealth(attacker))
				amount = GetClientHealth(attacker)-1;
			
			if(amount <= 0)
				return;
			
			DealDamage(attacker, amount, victim, _, "mirror");
		}
	}
}